package org.houor.utility;

/**
 * 数字工具类
 * 
 * @author Houor
 *
 * @createTime 2015-01-01 20:24
 */
public class NumberUtility {

	/**
	 * 获取某一位值
	 * 
	 * @param value
	 * @param index
	 * @return
	 */
	public static int getBitOfByte(byte value, int index) {
		byte result = (byte) (value & (0x01 << index));
		if (result == 0) {
			return 0;
		} else {
			return 1;
		}
	}

	public static int[] byte2IntArray(byte value) {
		int[] result = new int[8];
		for (int i = 0; i < 8; i++) {
			result[i] = getBitOfByte(value, i);
		}
		return result;
	}

	/**
	 * 数组转short
	 * 
	 * @param byteArray
	 * @return
	 */
	public static short byteArray2Short(byte[] byteArray) {
		short shortValue = 0;
		for (int i = 0; i < byteArray.length; i++) {
			shortValue += (byteArray[i] & 0xFF) << (8 * (1 - i));
		}
		return shortValue;
	}

	/**
	 * 数组转int
	 * 
	 * @param byteArray
	 * @return
	 */
	public static int byteArray2Int(byte[] byteArray) {
		int intValue = 0;
		for (int i = 0; i < byteArray.length; i++) {
			intValue += (byteArray[i] & 0xFF) << (8 * (3 - i));
		}
		return intValue;
	}

	/**
	 * 数组转long
	 * 
	 * @param byteArray
	 * @return
	 */
	public static long byteArray2Long(byte[] byteArray) {
		long longValue = 0;
		for (int i = 0; i < byteArray.length; i++) {
			longValue += ((long) (byteArray[i] & 0xFF)) << (8 * (7 - i));
		}
		return longValue;
	}

	/**
	 * short转数组
	 * 
	 * @param shortValue
	 * @return
	 */
	public static byte[] short2ByteArray(short shortValue) {
		byte[] b = new byte[2];
		for (int i = 0; i < 2; i++) {
			b[i] = (byte) (shortValue >> 8 * (1 - i) & 0xFF);
		}
		return b;
	}

	/**
	 * int转数组
	 * 
	 * @param intValue
	 * @return
	 */
	public static byte[] int2ByteArray(int intValue) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);
		}
		return b;
	}

	/**
	 * long转数组
	 * 
	 * @param longValue
	 * @return
	 */
	public static byte[] long2ByteArray(long longValue) {
		byte[] b = new byte[8];
		for (int i = 0; i < 8; i++) {
			b[i] = (byte) (longValue >> 8 * (7 - i) & 0xFF);
		}
		return b;
	}

	/**
	 * 数组水平旋转
	 * 
	 * @param byteArray
	 * @return
	 */
	public static byte[] rotateByteArray(byte[] byteArray) {
		int length = byteArray.length;
		byte[] result = new byte[length];
		for (int i = 0; i < length; i++) {
			result[length - i - 1] = byteArray[i];
		}
		return result;
	}

	/**
	 * 数组水平旋转
	 * 
	 * @param chars
	 * @return
	 */
	public static char[] rotateCharArray(char[] chars) {
		int length = chars.length;
		char[] result = new char[length];
		for (int i = 0; i < length; i++) {
			result[length - i - 1] = chars[i];
		}
		return result;
	}

	public static void initByteArray(byte[] byteArray) {
		for (int i = 0; i < byteArray.length; i++) {
			byteArray[i] = 0;
		}
	}

	public static String byteArrayToString(byte[] byteArray) {
		StringBuffer sb = new StringBuffer();
		for (byte b : byteArray) {
			sb.append(b + "\t");
		}
		return sb.toString();
	}

	public static String formatStringTo2Length(String str) {
		if (str.length() > 2)
			return str.substring(str.length() - 2, str.length());
		else if (str.length() == 2) {
			return str;
		} else {
			return "0" + str;
		}

	}

	/**
	 * 将整形数值转换为固定长度的字符串(包括符号, 长度不够则补0)
	 * 
	 * @param i
	 * @return
	 */
	public static String formatIntToStringByLength(int num, int length) {
		// FIXME 负号放在哪个位置
		byte[] chs = new byte[length];
		for (int i = 0; i < length; i++)
			chs[i] = 0x30;
		byte[] numBa = rotateByteArray(String.valueOf(Math.abs(num)).getBytes());
		System.arraycopy(numBa, 0, chs, 0, numBa.length);
		chs = rotateByteArray(chs);
		if (num < 0) {
			chs[0] = '-';
		}

		return new String(chs);

	}

	public static void main(String[] args) {
		int random = 2027723085;
		short r = 12345;

		System.out.println(byteArrayToString(int2ByteArray(random)));

		System.out.println(byteArrayToString(short2ByteArray(r)));
		System.out.println(getBitOfByte((byte) 0x02, 0));
		System.out.println(getBitOfByte((byte) 0x02, 1));
		System.out.println(getBitOfByte((byte) 0x02, 2));
		System.out.println(getBitOfByte((byte) 0x04, 2));

		System.out.println(String.valueOf(Math.abs((int) (35.23))));
		System.out.println(String.valueOf((int) (-35.23)));

		System.out.println(formatIntToStringByLength(35, 7));
		System.out.println(formatIntToStringByLength(-35, 7));

		System.out.println(Long.toHexString(4851527992737857537L));

	}

}
